package com.lsmo.android.carfans.activity.car;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.lsmo.android.carfans.R;
import com.lsmo.android.carfans.model.car.Brand;
import com.lsmo.android.carfans.process.car.BrandListProcess;
import com.lsmo.android.carfans.view.car.SelectCarView;
import com.lsmo.android.common.base.BaseActivity;
import com.lsmo.android.common.base.BaseProcess;
import com.lsmo.android.common.base.BaseTask;
import com.lsmo.android.common.base.ViewProcessCallBack;

public class CarPriceRangeListActivity extends BaseActivity 
	implements AdapterView.OnItemClickListener, View.OnClickListener,ViewProcessCallBack {
	
	public static final String TAG = SelectCarView.class.getSimpleName();
	
	public ListView mListView;
	public Context mContext ;
	private ViewGroup mProgressBarLayout;
	private ArrayList<Brand> BrandList;
	private BrandListProcess bListProcess;
	private String[] params = null;
	public static int ids,positions;
	public static String names;
	
	
	public static void launch(Context c,int position,int id,String name){
		Intent intent = new Intent(c, CarPriceRangeListActivity.class);
		ids = id;
		positions = position;
		names = name;
		c.startActivity(intent);
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.bListProcess = new BrandListProcess();
		this.mContext = CarPriceRangeListActivity.this;
		
		initView();//加载view
		
		updateView();//加载页面数据
	}


	public void initView(){
		this.setContentView(R.layout.select_view_only_list);
		mListView = (ListView)this.findViewById(R.id.listView);
		mListView.setClickable(true);
		mListView.setOnItemClickListener(this);//注册listItem监听
 		mProgressBarLayout = (ViewGroup)this.findViewById(R.id.loading_layout);
 		
 		TextView titleView = (TextView)this.findViewById(R.id.title_bar_layout_tv_id);
 		titleView.setGravity(Gravity.CENTER);
 		titleView.setText(names);
	}


	@Override
	public void processAfterTaskFinish(int code, Object result) {
		// TODO Auto-generated method stub
		
		if(code == CarPriceRangeTask.GET_CAR_PRCIE_RANGE_LIST){
			if(result!=null){
				refreshView();
			}
		}
		this.mProgressBarLayout.setVisibility(View.GONE);
		this.mListView.setVisibility(View.VISIBLE);
	}
	
	
	@Override
	public void updateView() {
		// TODO Auto-generated method stub
		
		this.mProgressBarLayout.setVisibility(View.VISIBLE);
		this.mListView.setVisibility(View.GONE);
		   
			new CarPriceRangeTask(CarPriceRangeTask.GET_CAR_PRCIE_RANGE_LIST,
					this.mContext,
					this,
					this.bListProcess 
		 ).execute(new String[]{});
	}
	
	
	private class CarPriceRangeTask extends BaseTask{
		public static final int GET_CAR_PRCIE_RANGE_LIST =9;
		private BrandListProcess bListProcess ;
		
		public CarPriceRangeTask(int code, Context context,
						ViewProcessCallBack view,BaseProcess process) {
			super(code, context, view);
			// TODO Auto-generated constructor stub
			this.bListProcess =(BrandListProcess)process;
		}
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		
		@Override
		protected Object doInBackground(String... params) {
			super.doInBackground(params);
			Object result = null;
	 
			switch(this.mCode){
			    
				case GET_CAR_PRCIE_RANGE_LIST:
					
					//加载数据
					result = (ArrayList<Brand>) bListProcess.getCarPriceRangeList(params, ids,names);
					BrandList = (ArrayList<Brand>) result;
					
					break;
			}
			return result;
		}
		
		@Override
		protected void onPostExecute(Object result) {
			super.onPostExecute(result);
			switch(this.mCode){
				 
				case GET_CAR_PRCIE_RANGE_LIST:
					
				 		mView.processAfterTaskFinish(this.mCode ,result );
				 	
				 	break;
		   }
		}
		
		
	}
	
	
	/**
	 * 刷新页面数据
	 */
	public void refreshView(){
		BrandListAdapter brandListAdapter  = new BrandListAdapter(this);
		brandListAdapter.setList(BrandList);
		
		SeparatedListAdapter separatedAdapter = new SeparatedListAdapter(this);
		separatedAdapter.addSection(names, brandListAdapter);
		
		mListView.setAdapter(separatedAdapter);
	}
	
	
	/**
	 * listview 监听事件
	 * @param method
	 * @param param
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		if(arg2<=9){
			int id = ((Brand)BrandList.get(arg2)).getId();
		    String name = ((Brand)BrandList.get(arg2)).getName();
			BrandTypeActivity.launch(mContext,arg2,id,name);
		}
	}


	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		
	}
	
	
	
	
}
